﻿using System;
using System.Text.RegularExpressions;

namespace MSAcademic.v2.Infrastructure.Utility
{
    public class RandomNonceGenerator
    {
        public string GetNewWithoutSpecialChars(int length)
        {
            var buffer = _GetRandomlyFilledBuffer(length);
            var unfilteredString = Convert.ToBase64String(buffer);

            return Regex.Replace(unfilteredString, "[^a-zA-Z0-9]+", "x", RegexOptions.Compiled);
        }

        public string GetNewOfRandomLength()
        {
            var random = new Random();
            var buffer = _GetRandomlyFilledBuffer(random.Next(10, 20));
            return Convert.ToBase64String(buffer);
        }

        private byte[] _GetRandomlyFilledBuffer(int bufferLength)
        {
            var buffer = new byte[bufferLength];
            new Random().NextBytes(buffer);
            return buffer;
        }
    }
}
